﻿using System.Linq;
using Core.DAL;
using Core.Utils;
using System;

namespace Core.BOL
{
    public class MailToGenerateRepository : IDisposable
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public MailToGenerateRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public tMail_ToGenerate GetMailById(int mid)
        {
            return context.tMail_ToGenerates.Where(p => p.ID == mid).SingleOrDefault();
        }

        public tMail_ToGenerate GetLatestMailToGenerate()
        {
            return context.tMail_ToGenerates.Where(p => p.MID == null).OrderBy(p => p.InsertDate).Take(1).SingleOrDefault();
        }

        public void InsertMail(tMail_ToGenerate mail)
        {
            context.tMail_ToGenerates.InsertOnSubmit(mail);
        }

        public void DeleteMail(tMail_ToGenerate mail)
        {
            context.tMail_ToGenerates.DeleteOnSubmit(mail);
        }

        public void Save()
        {
            context.SubmitChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }
        #endregion
    }
}
